package com.osgix.common.utils;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Enumeration;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 读取属性文件初始化类属性
 * 2010-2-2,上午03:29:12
 * @author azheng
 * @version 2.0
 */
public class StaticProperties {
	/**
	* Logger for this class
	*/
	private static final Logger logger = Logger.getLogger(StaticProperties.class);

	private String propFile;

	public void setPropFile(String propFile) {
		this.propFile = propFile;
	}

	/**
	 * 反射读取属性
	 * 2010-2-2,上午03:29:44
	 * @author azheng
	 * @param sqlClass
	 * @param fieldName
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private Object getStaticProperty(Class sqlClass, String fieldName) throws Exception {
		Field field = sqlClass.getField(fieldName);
		Object value = field.get(null);
		return value;
	}

	/**
	 * 反射设置属性值
	 * 2010-2-2,上午03:30:08
	 * @author azheng
	 * @param sqlClass
	 * @param fieldName
	 * @param value
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private void setStaticProperty(Class sqlClass, String fieldName, Object value) throws Exception {
		Field field = sqlClass.getField(fieldName);
		field.set(null, value);
	}

	/**
	 * 初始化指定的类中的属性
	 * 2010-2-2,上午03:30:31
	 * @author azheng
	 * @param sqlClass
	 * @param prop
	 */
	@SuppressWarnings("unchecked")
	private void initSql(Class sqlClass, Properties prop) {
		//通过静态方法设置属性
		//System.out.println("--------------- static block ---------------");
		try {
			Enumeration enums = prop.keys();
			while (enums.hasMoreElements()) {
				String key = (String) enums.nextElement();
				String[] keyName = key.split("\\.");
				if (keyName.length != 2)
					continue;//其他配置项掠过
				String cname = keyName[0];
				String pname = keyName[1];
				if (getClassName(sqlClass.getName()).equals(cname)) {
					//如果属性key的类名部分相同,则赋值
					String value = prop.getProperty(key);
					setStaticProperty(sqlClass, pname, value);
					//System.out.println(key + " = " + getStaticProperty(sqlClass, pname));
				}
			}
		} catch (NoSuchFieldException nsfe) {
			if (logger.isInfoEnabled()) {
				logger.info("初始化属性:未在类中找到对应的属性");
			}
			nsfe.printStackTrace();
		} catch (Exception e) {
			if (logger.isInfoEnabled()) {
				logger.info("初始化属性:设置属性值时发生错误");
			}
			e.printStackTrace();
		}
	}

	/**
	 * 读取静态文件列表,逐个类初始化
	 * 2010-2-2,上午03:30:31
	 * @author azheng
	 */
	@SuppressWarnings("unchecked")
	public void init() {
		Properties prop = new Properties();
		InputStream in = StaticProperties.class.getResourceAsStream(propFile);
		try {
			prop.load(in);
		} catch (IOException e1) {
			if (logger.isInfoEnabled()) {
				logger.info("初始化属性:属性文件" + propFile + "读取失败");
			}
			//文件读取异常
			e1.printStackTrace();
		}
		//取得要设置属性的类
		String classes = prop.getProperty("init.classes");
		String[] classList = classes.split(",");
		//逐个类初始化
		for (String classFullName : classList) {
			try {
				Class clazz = Class.forName(classFullName);
				initSql(clazz, prop);//执行初始化方法
				if (logger.isInfoEnabled()) {
					logger.info("初始化属性:[" + classFullName + "] 从" + propFile + "读取SQL成功");
				}
			} catch (ClassNotFoundException e) {
				//设置属性异常
				if (logger.isInfoEnabled()) {
					logger.info("初始化属性:通过类的全路径,未找到相应的类");
				}
				e.printStackTrace();
			}
		}

	}

	/**
	 * 通过类全路径名获得类名
	 * 2010-2-2,上午06:04:51
	 * @author azheng
	 * @param classFullName
	 * @return
	 */
	private static String getClassName(String classFullName) {
		String className = null;
		if (classFullName != null) {
			String[] sname = classFullName.split("\\.");
			className = sname[sname.length - 1].trim();
		}
		return className;
	}
}
